<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin_khmerhr extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	function __construct(){
		parent:: __construct();
		$this->load->library('form_validation');
		$this->load->model('mod_khmerhr_admin');
		$this->load->helper('khmerhr_widget');
		$this->loggedIn();
	}
	protected function loggedIn(){
		if (!$this->session->userdata('admin_loggedIn'))
		  {
		  redirect('loginAdmin');
		  }
	}
	function logout(){
		$this->session->sess_destroy();
		redirect('loginAdmin');
	}
	function viewEmployee($offest=NULL){
		$data['title'] = "View Employee";
		$data['page'] = 'viewEmployee';		

        $limit                   = 10;
        $segment                 = 3;
        $num_link                = 5;
        $total_rows              = $this->mod_khmerhr_admin->count_table("tbl_users");
        $base_url                = base_url()."index.php/admin_khmerhr/viewEmployee";
        pagination($limit, $segment, $base_url, $total_rows, $num_link);
       	$data['employee_data'] = $this->mod_khmerhr_admin->getUser($limit, $offest);
       
        $data['links']           = $this->pagination->create_links();

		$this->load->view("index_admin",$data);
	}
	function viewJobSeekers(){
		$data['title'] = "View Job Seekers";
		$data['page'] = 'viewJobSeekers';
		$data['employee_data'] = $this->mod_khmerhr_admin->getUser("jobseeker");
		$this->load->view("index_admin",$data);
	}
	function jobCategories($offest=NULL){
		$data['title'] = "Job Categories";
		$data['page'] = 'jobCategories';	

		$limit                   = 10;
        $segment                 = 3;
        $num_link                = 5;
        $total_rows              = $this->mod_khmerhr_admin->count_table("tbl_jobtype");
        $base_url                = base_url()."index.php/admin_khmerhr/jobCategories";
        pagination($limit, $segment, $base_url, $total_rows, $num_link);
       	$data['cateogry_data'] = $this->mod_khmerhr_admin->getCategory($limit, $offest);
       
        $data['links']           = $this->pagination->create_links();
        $data["toalCate"] =  $total_rows;
		
		$this->load->view("index_admin",$data);
	}

	function locations($offest=NULL){
		$data['title'] = "Locations";
		$data['page'] = 'location';
		$limit                   = 10;
        $segment                 = 3;
        $num_link                = 5;
        $total_rows              = $this->mod_khmerhr_admin->count_table("tbl_location");
        $base_url                = base_url()."index.php/admin_khmerhr/locations";
        pagination($limit, $segment, $base_url, $total_rows, $num_link);
       	$data['location_data'] = $this->mod_khmerhr_admin->getLocation($limit, $offest);
       
        $data['links']           = $this->pagination->create_links();
        $data["totalLocation"] = $total_rows;
		
		$this->load->view("index_admin",$data);
	}
	function deleteLocation($id){
		$result = $this->mod_khmerhr_admin->deleteLocation($id);
		if($result){
          echo "Success";
        }else{
          echo "Error";
        }
	}




	function addCategory(){
		$name = $this->input->post("name");
		$description = $this->input->post("description");
		$this->mod_khmerhr_admin->addCategory($name,$description);
		
		$this->session->set_flashdata("message_success",'<div class="alert alert-success alertText" role="alert">
						  <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
						  <strong>Well done! </strong> You successfully add category.
						</div>' );
		redirect("admin_khmerhr/jobCategories");
	}
	function addLocation(){
		$name = $this->input->post("name");
		$description = $this->input->post("description");
		$this->mod_khmerhr_admin->addLocation($name,$description);
		
		$this->session->set_flashdata("message_success",'<div class="alert alert-success alertText" role="alert">
						  <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
						  <strong>Well done! </strong> You successfully add location.
						</div>' );
		redirect("admin_khmerhr/locations");
	}
	function deleteCategory($idCate){
		
		$result = $this->mod_khmerhr_admin->deleteCategory($idCate);
		if($result){
          echo "Success";
        }else{
          echo "Error";
        }
	}
	
	function viewDetailUser($id){
		$datas = $this->mod_khmerhr_admin->getDetailUser($id)->result();
		header('Content-Type: application/json');
        echo json_encode($datas);
	}
	function  getDetailLocation($id){
		$datas = $this->mod_khmerhr_admin->getDetailLocation($id)->row();
		header('Content-Type: application/json');
        echo json_encode($datas);
	}

function  getDetailCategory($id){
		$datas = $this->mod_khmerhr_admin->getDetailCategory($id)->row();
		header('Content-Type: application/json');
        echo json_encode($datas);
	}


	function deleteEmployee($id){
		$result = $this->mod_khmerhr_admin->deleteEmployee($id);
		if($result){
          echo "Success";
        }else{
          echo "Error";
        }
	}
	function updateUser($id){
		$firstName = $this->input->post("firstName");
		$lastName = $this->input->post("lastName");
		$company = $this->input->post("companyName");
		$contactPerson = $this->input->post("contactPerson");
		$phone = $this->input->post("phone");
		
		$type = $this->input->post("type");
		$this->mod_khmerhr_admin->updateUserProfile($firstName,$company,$contactPerson,$phone,$lastName,$type,$id);
		$this->session->set_flashdata("message_success","You are successfully");
		redirect("admin_khmerhr/viewEmployee");
	}

	function getCompanyId(){
		$id = $this->input->post("val");
		$datas = $this->mod_khmerhr_admin->getCompanyId($id)->row();
		header('Content-Type: application/json');
        echo json_encode($datas);
	}
	function postImange(){
		$data["page"] = "upload";
		$config['upload_path']          = './public/images/logo/';
        $config['allowed_types']        = 'gif|jpg|png';
        


        $this->load->library('upload', $config);
        $this->upload->initialize($config);
        if (!$this->upload->do_upload('userfile')) {
			$this->load->view("index_admin",$data);
		}else{
			echo "hello";
		}
	}
	function postJobNewCompany(){
		 $this->form_validation->set_rules('comName', 'Company Name', 'required');
		$this->form_validation->set_rules('jobType', 'Job Type', 'required');
		$this->form_validation->set_rules('jobTitle', 'Job Title', 'required');
		$this->form_validation->set_rules('provinces', 'Province', 'required');
		$this->form_validation->set_rules('cates', 'Categories', 'required');
		$this->form_validation->set_rules('post_date', 'Post Date', 'required');
		$this->form_validation->set_rules('close_date', 'Close Date', 'required');
		$this->form_validation->set_rules('employee', 'Employee', 'required');
		
	  	$config['upload_path']          = './public/images/logo/';
        $config['allowed_types']        = 'gif|jpg|png';
        $this->load->library('upload', $config);
       
		if($this->form_validation->run() === false && !$this->upload->do_upload('userfile')){
			header('Content-Type: application/json');
	   		echo json_encode(array(
	   			'st' =>0,
	   			'comName'=>form_error('comName'),
	   			'jobType'=>form_error('jobType'),
	   			'jobTitle'=>form_error('jobTitle'),
	   			'provinces'=>form_error('provinces'),
	   			'cates'=>form_error('cates'),
	   			'post_date'=>form_error('post_date'),
	   			'close_date'=>form_error('close_date'),
	   			'employee'=>form_error('employee'),
	   			'userfile' => $this->upload->display_errors()
	   			
	   			));
		}else{ 

	        	$upload_data = $this->upload->data();
				$comName = $this->input->post("comName");
				$jobType = $this->input->post("jobType");
				$jobTitle = $this->input->post("jobTitle");
				$companyDes = $this->input->post("aboutCompany");
				$salary = $this->input->post("salary");
				$hiring = $this->input->post("hiring");
				$provinces = $this->input->post("provinces");
				$cate = $this->input->post("cates");
				$duty = $this->input->post("duty");
				$requirement = $this->input->post("requirement");
				$howToApply = $this->input->post("howToApply");
				$date_post = $this->input->post("post_date");
				$date_close = $this->input->post("close_date");
				$employee = $this->input->post("employee");
				$this->mod_khmerhr_admin->addPostJob($comName,$jobType,$jobTitle,$companyDes,$salary,$hiring,$provinces,$cate,$duty,$requirement,$howToApply,$date_post,$date_close,$employee);
				
				$this->session->set_flashdata("message_success",'<div class="alert alert-success alertText" role="alert">
							  <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
							  <strong>Well done! </strong> You successfully posting job.
							</div>' );
				header('Content-Type: application/json');
		   		echo json_encode(array(
		   			'st' =>1,'employee'=>$employee));


	        
           
			
		}
	}
	function savePostJob(){
	    $this->form_validation->set_rules('comName', 'Company Name', 'required');
		$this->form_validation->set_rules('jobType', 'Job Type', 'required');
		$this->form_validation->set_rules('jobTitle', 'Job Title', 'required');
		$this->form_validation->set_rules('provinces', 'Province', 'required');
		$this->form_validation->set_rules('cates', 'Categories', 'required');
		$this->form_validation->set_rules('post_date', 'Post Date', 'required');
		$this->form_validation->set_rules('close_date', 'Close Date', 'required');
		$this->form_validation->set_rules('employee', 'Employee', 'required');
		if($this->form_validation->run() === false){
			header('Content-Type: application/json');
	   		echo json_encode(array(
	   			'st' =>0,
	   			'comName'=>form_error('comName'),
	   			'jobType'=>form_error('jobType'),
	   			'jobTitle'=>form_error('jobTitle'),
	   			'provinces'=>form_error('provinces'),
	   			'cates'=>form_error('cates'),
	   			'post_date'=>form_error('post_date'),
	   			'close_date'=>form_error('close_date'),
	   			'employee'=>form_error('employee')
	   			
	   			));
		}else{ 
			$comName = $this->input->post("comName");
			$jobType = $this->input->post("jobType");
			$jobTitle = $this->input->post("jobTitle");
			$salary = $this->input->post("salary");
			$hiring = $this->input->post("hiring");
			$provinces = $this->input->post("provinces");
			$cate = $this->input->post("cates");
			$duty = $this->input->post("duty");
			$requirement = $this->input->post("requirement");
			$howToApply = $this->input->post("howToApply");
			$date_post = $this->input->post("post_date");
			$date_close = $this->input->post("close_date");
			$employee = $this->input->post("employee");
			$this->mod_khmerhr_admin->addPostJobExit($comName,$jobType,$jobTitle,$salary,$hiring,$provinces,$cate,$duty,$requirement,$howToApply,$date_post,$date_close,$employee);
			
			$this->session->set_flashdata("message_success",'<div class="alert alert-success alertText" role="alert">
						  <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
						  <strong>Well done! </strong> You successfully posting job.
						</div>' );
			
			
			header('Content-Type: application/json');
	   		echo json_encode(array(
	   			'st' =>1));

		}
	}

	function postJob(){
        $data['title'] = "Post Job";
	    $data['page'] = 'postJob';
	    $data['companyExits'] = $this->mod_khmerhr_admin->getCompanyExit($this->session->userdata("userId"));
	   
	   	$this->load->view("index_admin",$data);
		
    }
	function addUsers(){
		$data['title'] = "Add Users";
		$data['page'] = 'addUsers';
		$this->form_validation->set_rules('firstName', 'First Name', 'required');
		$this->form_validation->set_rules('lastName', 'Last Name', 'required');
		$this->form_validation->set_rules('type', 'Position', 'required');
		$this->form_validation->set_rules('email', 'E-mail', 'required');
		$this->form_validation->set_rules('address', 'Address', 'required');
		if($this->form_validation->run() != false){
			$firstName = $this->input->post('firstName');
			$lastName = $this->input->post('lastName');
			$type = $this->input->post('type');
			$email = $this->input->post('email');
			$phone = $this->input->post('phone');
			$companyName = $this->input->post('companyName');
			$contactPerson = $this->input->post('contactPerson');
			$address = $this->input->post("address");
			$this->mod_khmerhr_admin->addUsers($firstName,$lastName,$type,$email,$phone,$companyName,$contactPerson,$address);
			redirect("admin_khmerhr/viewEmployee");
		}else{ 
			$this->load->view("index_admin",$data);
		}

	}
	/** Developer Thymeak **/
 	function updatepostjob($jobId){
		
			$comName = $this->input->post("comName");
			$jobType = $this->input->post("jobType");
			$jobTitle = $this->input->post("jobTitle");
			$salary = $this->input->post("salary");
			$hiring = $this->input->post("hiring");
			$provinces = $this->input->post("provinces");
			$cate = $this->input->post("cates");
			$duty = $this->input->post("duty");
			$requirement = $this->input->post("requirement");
			$howToApply = $this->input->post("howToApply");
			$date_post = $this->input->post("post_date");
			$date_close = $this->input->post("close_date");
			$employee = $this->input->post("employee");
			$this->mod_khmerhr_admin->updatepostjob($jobId,$comName,$jobType,$jobTitle,$salary,$hiring,$provinces,$cate,$duty,$requirement,$howToApply,$date_post,$date_close,$employee);
			
			$this->session->set_flashdata("message_success",'<div class="alert alert-success alertText" role="alert">
						  <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
						  <strong>Well done! </strong> You are successfully .
						</div>' );
					
			
			redirect("admin_khmerhr/viewAllJob");
		// }


 	 }

	function editPostJob($jobId){
		$data['title'] = "Edit Post Job";
		$data['page'] = 'editPostJob';
		$arr = explode("_", $jobId);
			$data["result"] = $this->mod_khmerhr->getViewJobDetail($arr[1]);
			$data['companyExits'] = $this->mod_khmerhr_admin->getCompanyExit($this->session->userdata("userId"));
			$this->load->view("index_admin",$data);
	}

	function viewAllJob($offest=NULL){

        $data['title'] = "viewAllJob";
		$data['page'] = 'viewAllJob';		

        $limit                   = 10;
        $segment                 = 3;
        $num_link                = 5;
        $total_rows              = $this->mod_khmerhr_admin->count_table("tbl_job_post");
        $base_url                = base_url()."index.php/admin_khmerhr/viewAllJob";
        pagination($limit, $segment, $base_url, $total_rows, $num_link);
       	$data['job_data'] = $this->mod_khmerhr_admin->getViewJob($limit, $offest);
       
        $data['links'] = $this->pagination->create_links();

		$this->load->view("index_admin",$data);

	}

	function deletejob($jobid){

		$this->mod_khmerhr_admin->deletejob($jobid);
		redirect("admin_khmerhr/viewAllJob");

	}

	function editLocation(){
		$id = $this->input->post("idLocation");
		$name = $this->input->post("name");
		$des = $this->input->post("description");
		$this->mod_khmerhr_admin->editLocation($id,$name,$des);
		$this->session->set_flashdata("message_success","You are successfully");
		redirect("admin_khmerhr/locations");
	}



	function editCategory(){
        $id = $this->input->post("idCategory");
		$name = $this->input->post("name");
		$des = $this->input->post("description");
		$this->mod_khmerhr_admin->editCategory($id,$name,$des);
		$this->session->set_flashdata("message_success","You are successfully");
		redirect("admin_khmerhr/jobCategories");
	}
	/** End of developer **/




}